package com.spring.shops.admin.controller.shop;

import com.spring.shops.model.entity.TzArea;
import com.spring.shops.model.entity.TzTransfee;
import com.spring.shops.model.entity.TzTransfeeFree;
import com.spring.shops.model.entity.TzTransport;
import com.spring.shops.model.result.ServerResponseEntityVoid;
import com.spring.shops.model.vo.TransportVO2;
import com.spring.shops.model.vo.TransportVo;
import com.spring.shops.service.TransportService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/shop")
public class TransportController {

    @Resource
    private TransportService transportService;

    @GetMapping("/transport/page")
    public ServerResponseEntityVoid seleteTransportAll(@RequestParam(required = false) String transName,
                                                       @RequestParam int current,
                                                       @RequestParam int size){
        if (transName==null){
            transName="1";
        }else {
            transName="%"+transName+"%";
        }
        TransportVo transportVo = transportService.selectTransportsPage(current, size,transName);
        return ServerResponseEntityVoid.success(transportVo);
    }

    @PutMapping("/transport")
    public ServerResponseEntityVoid update(@RequestBody TzTransport transport){
        transportService.updateTzTransport(transport);
        return ServerResponseEntityVoid.success();
    }

    @PostMapping("/transport")
    public ServerResponseEntityVoid insert(@RequestBody TzTransport tzTransport){
        tzTransport.setCreateTime(new Date());
        transportService.insertTzTransport(tzTransport);
        return ServerResponseEntityVoid.success();
    }

   // @GetMapping("/transport/info/{id}")
//    public ServerResponseEntityVoid update(@PathVariable("id") int id){
//        TzTransport transport = transportService.selectById((long) id);
//        TzTransfeeFree[] transfeeFrees =new TzTransfeeFree[0];
//        TzTransfee[] Transfees =new TzTransfee[1];
//        TzArea[] tzAreas =new TzArea[1];
//        TzTransfee transfee =new TzTransfee();
//        transfee.setTransfeeId((long)115);
//        transfee.setTransportId((long)47);
//        BigDecimal bc = new BigDecimal("2.0");
//        transfee.setContinuousPiece(bc);
//        transfee.setFirstPiece(bc);
//        transfee.setContinuousFee(bc);
//        transfee.setFirstFee(bc);
//        Transfees[0]=transfee;
//        TzArea tzArea =new TzArea();
//        tzArea.setAreaId((long) 1);
//        tzArea.setAreaName("北京");
//        tzArea.setAreas("中山区");
//        tzArea.setLevel(1);
//        tzArea.setParentId((long) 1);
//        tzAreas[0]=tzArea;
//        transfee.setCityList(tzAreas);
//        transport.setTransfees(Transfees);
//        transport.setTransfeeFrees(transfeeFrees);
//        return ServerResponseEntityVoid.success(transport);
//    }

    @DeleteMapping("/transport")
    public ServerResponseEntityVoid deleteByTranId(@RequestBody int[] ids){
        for (int id:ids){
            int i = transportService.deletedByTzTransportId((long) id);
        }
        return ServerResponseEntityVoid.success();
    }
    /**
     * 获取所有的运费模板
     */
    @GetMapping("/transport/list")
    public ServerResponseEntityVoid<List<TransportVO2>> list(){
       return transportService.list();
    }
        /**
     * 根据运费模板id获取具体信息
     * @param id
     * @return
     */
    @GetMapping("/transport/info/{id}")
    public ServerResponseEntityVoid<TransportVO2> getInfoById(@PathVariable Long id){
        return transportService.getInfoById(id);
    }
}

